﻿using BUS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWeb.GUI.User
{
    public partial class GioHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadCart();
            }
        }

        private void LoadCart()
        {
            liTotal.Text = string.Format("{0:0,0}", ShoppingCart.Instance.GetSubTotal());
            if (ShoppingCart.Instance.Items.Count > 0)
            {
                rpGioHang.DataSource = ShoppingCart.Instance.Items;
                rpGioHang.DataBind();
            }
            else
            {
                Response.Write("<script>alert('Bạn chưa chon sản phẩm nào');window.location='TrangChu.aspx'</script>");
            }
        }

        protected void rpGioHang_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            TextBox curqty = (TextBox)e.Item.FindControl("txtquantity");
            int newqty = int.Parse(curqty.Text);
            if (e.CommandName.Equals("update"))
            {
                int masp = Convert.ToInt32(e.CommandArgument);
                ShoppingCart.Instance.SetItemQuantity(masp, newqty);
                LoadCart();
            }

            if (e.CommandName.Equals("delete"))
            {
                int masp = Convert.ToInt32(e.CommandArgument);
               ShoppingCart.Instance.RemoveItem(masp);
                LoadCart();
            }
            
        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            Response.Redirect("SanPham.aspx");
        }

        protected void btncheckout_Click(object sender, EventArgs e)
        {

        }
    }
}